﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;  //path
using System.Text.RegularExpressions;

namespace proj001
{
    class XPaidUnpaidParser
    {
        /* ************ Data Member ******************* */
        private string m_filename;
        private List<int> m_currencyIndex;

        public XPaidUnpaidParser()
        {
            m_currencyIndex = new List<int>();
        }
 
        /* ************ Member Function *************** */
        public EErrorCode parseExcel(string in_filename)
        {
            if (in_filename == null)
                return EErrorCode.INVALID_FILE_NAME;
            string tmpExt = Path.GetExtension(in_filename);
            if (tmpExt != ".xls")
                return EErrorCode.INVALID_FILE_EXTENSION;
            //save into data member
            m_filename = in_filename;
            object misVal = System.Reflection.Missing.Value;

            //open excel file
            Excel.Application xlsApp = new Excel.ApplicationClass();
            Excel.Workbook xlsWorkbook;
            Excel.Worksheet xlsworksheet;
            Excel.Range xlsRange;

            xlsWorkbook = xlsApp.Workbooks.Open(
                    m_filename,
                    misVal,true,misVal,"","",
                    misVal,Excel.XlPlatform.xlWindows,"\t",
                    false,false,false,misVal,misVal,false
                );

            xlsworksheet = (Excel.Worksheet)xlsWorkbook.Worksheets.get_Item(1);
            xlsRange = xlsworksheet.UsedRange;
            
            //get column and row
            int totalColumn = xlsRange.Columns.Count;
            int totalRow = xlsRange.Rows.Count;
    
            Excel.Range ColumnOne = xlsworksheet.get_Range("A1","A"+totalRow);
            // Find out currency's appear times
            int index = 1;
            
            foreach (object element in (object [,])ColumnOne.Value2)
            {   
                if (null != element)
                {
                    if (element.GetType() == typeof(string))
                    {
                        Match matchCurrency = Regex.Match((string)element, @"^Currency.*$", RegexOptions.IgnoreCase);
                        if (matchCurrency.Success)
                        {                  
                            string[] tmpArr = ((string)element).Split(':');
                            tmpArr[1] = tmpArr[1].Replace(" ", "");
                            m_currencyIndex.Add(index);
                         }                     
                    }    
                }
                index++;
            }

            //Todo: Analysis Block (currency)
            Excel.Range currencyRange = xlsworksheet.get_Range("A1", "I27");


            //Clean up
            xlsWorkbook.Close(false, m_filename, misVal);
            xlsApp.Quit();

            return 0;
        }

        public void log()
        {
            Console.WriteLine("Range:\n");
            for (int i = 0; i < m_currencyIndex.Count; i++)
            {
                Console.WriteLine(i + " Block: " + " A" + m_currencyIndex[i] + "\n");
            }
             
            Console.ReadLine();

        }
    }
}
